package com.example.sociamedia.post.controller;

import com.example.sociamedia.post.dto.LikeRequest;
import com.example.sociamedia.post.dto.LikeResponse;
import com.example.sociamedia.post.entity.Like;
import com.example.sociamedia.post.mapper.LikeMapper;
import com.example.sociamedia.user.entity.User;
import com.example.sociamedia.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class LikeService implements LikeApi {
    private final LikeMapper likeMapper;
    private final UserMapper userMapper;

    @Autowired
    public LikeService(LikeMapper likeMapper, UserMapper userMapper) {
        this.likeMapper = likeMapper;
        this.userMapper = userMapper;
    }

    @Override
    public LikeResponse likePost(String sessionToken, LikeRequest request) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            LikeResponse resp = new LikeResponse();
            resp.setSuccess(false);
            resp.setMessage("无效的用户token");
            return resp;
        }
        
        // 检查是否已点赞
        Like exist = likeMapper.selectLike(request.getPostId(), user.getUserid());
        if (exist != null) {
            LikeResponse resp = new LikeResponse();
            resp.setSuccess(false);
            resp.setMessage("已点赞该帖子");
            return resp;
        }
        
        // 插入点赞记录
        Like like = new Like();
        like.setPostId(request.getPostId());
        like.setUserId(user.getUserid());
        likeMapper.insertLike(like);
        
        LikeResponse resp = new LikeResponse();
        resp.setSuccess(true);
        resp.setMessage("点赞成功");
        resp.setLikeId(like.getLikeId());
        return resp;
    }

    @Override
    public LikeResponse unlikePost(String sessionToken, LikeRequest request) {
        User user = userMapper.selectUserByToken(sessionToken);
        if (user == null) {
            LikeResponse resp = new LikeResponse();
            resp.setSuccess(false);
            resp.setMessage("无效的用户token");
            return resp;
        }
        
        int count = likeMapper.deleteLike(request.getPostId(), user.getUserid());
        LikeResponse resp = new LikeResponse();
        if (count > 0) {
            resp.setSuccess(true);
            resp.setMessage("取消点赞成功");
        } else {
            resp.setSuccess(false);
            resp.setMessage("未点赞该帖子");
        }
        return resp;
    }

    @Override
    public int getLikeCount(Long postId) {
        return likeMapper.countLikesByPost(postId);
    }

    @Override
    public List<Like> getLikesByPost(Long postId) {
        return likeMapper.selectLikesByPost(postId);
    }

    @Override
    public List<Like> getLikesByUser(Long userId) {
        return likeMapper.selectLikesByUser(userId);
    }
} 